//
//  ArticleDetailViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 2016/10/20.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "ArticleDetailViewController.h"
#import "LoginViewController.h"

@interface ArticleDetailViewController ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webViewMain;
@end

@implementation ArticleDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"文章详情";
    [self showNavgationWithLeftItemWithTitle:@"返回"];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                    initWithURL:[NSURL URLWithString:self.strArticleURL]
                                    cachePolicy:NSURLRequestUseProtocolCachePolicy
                                    timeoutInterval:15.f];
    [_webViewMain loadRequest:request];
    [self getArticlecollectStart];
    [SVProgressHUD showWithStatus:@"加载中.." maskType:SVProgressHUDMaskTypeClear];
}

#pragma mark - 数据请求
- (void)getArticlecollectStart{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:UserDataID];
    if ([str isEqualToString:@""] || str == nil || str == NULL) {
        [dict setObject:@"0" forKey:@"user_id"];
    }else{
        [dict setObject:[[NSUserDefaults standardUserDefaults] objectForKey:UserDataID] forKey:@"user_id"];
    }
    [dict setObject:self.strArticleID forKey:@"article_id"];
    [[BaseDataRequest alloc] getArticlecollectStart:dict withBlock:^(id responseObject, NSString *status, NSError *error) {
        [SVProgressHUD dismiss];
        if ([status isEqualToString:@"0"]) {
            NSMutableDictionary *model = [responseObject objectForKey:@"result"];
            if ([[model objectForKey:@"iscollect"] intValue] == 1) {
                [self setRightBtn:@"icon_collection"];
            }else{
                [self setRightBtn:@"icon_uncollection"];
            }
        }else{
            if (error != nil) {
                TIPSFORVIEW(@"请检测网络设置");
            }else{
                TIPSFORVIEW([responseObject objectForKey:@"message"]);
            }
            
        }
    }];
}

- (void)articleCollect{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if (![self logInspection]) {
        [dict setObject:@"0" forKey:@"user_id"];
    }else{
        [dict setObject:[[NSUserDefaults standardUserDefaults] objectForKey:UserDataID] forKey:@"user_id"];
    }
    [dict setObject:self.strArticleID forKey:@"article_id"];
    [[BaseDataRequest alloc] articleCollect:dict withBlock:^(id responseObject, NSString *status, NSError *error) {
        [SVProgressHUD dismiss];
        if ([status isEqualToString:@"0"]) {
            if ([[[responseObject objectForKey:@"result"] objectForKey:@"iscollect"] isEqualToString:@"yes"]) {
                [self setRightBtn:@"icon_collection"];
            }else{
                [self setRightBtn:@"icon_uncollection"];
            }
            NSNotification *notification = [NSNotification notificationWithName:@"UNDATETABLEVIEW" object:nil userInfo:nil];
            [[NSNotificationCenter defaultCenter] postNotification:notification];
        }else{
            if (error != nil) {
                TIPSFORVIEW(@"请检测网络设置");
            }else{
                TIPSFORVIEW([responseObject objectForKey:@"message"]);
            }
        }
    }];
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [SVProgressHUD dismiss];
}

#pragma mark - UIAlertViewDelehgate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        LoginViewController *login = getStoryOfControllerInstance(@"LoginViewController");
        BaseNavController *baseNav = [[BaseNavController alloc] initWithRootViewController:login];
        [UIApplication sharedApplication].delegate.window.rootViewController = baseNav;
    }
}

#pragma mark - 私有方法
- (void)setRightBtn:(NSString *)imageName{
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    UIBarButtonItem *rigthBar = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    [rightBtn setFrame:CGRectMake(15, kNavigationBtnSpaceHorizontal, 30, 30)];
    [rightBtn addTarget:self action:@selector(like) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = rigthBar;
}

- (void)like{
    if (![self logInspection]) {
        return;
    }
    [self articleCollect];
}

- (BOOL)logInspection{
    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:UserDataID];
    if ([str isEqualToString:@""] || str == nil || str == NULL) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您尚未登录，是否立即登录" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
        alertView.delegate = self;
        [alertView show];
        return NO;
    }else{
        return YES;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
